package com.gua.jbs.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.gua.jbs.bean.UserBean;
import com.gua.jbs.dao.IUserDao;
import com.gua.jbs.daoImpl.UserDao;
import com.gua.jbs.pojo.Jbsult;
import com.gua.jbs.service.IUserService;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.EncryptionDecryption;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.StringUtil;

public class UserService extends BaseService implements IUserService{
	private static final Logger logger = Logger.getLogger(ProductService.class);

	public boolean ChangePassword(UserBean userBean)
			throws ApplicationException {
		boolean isEqualPwd= false;
		boolean updatePassword=false;
		String encodedPassword;
		String encodedNewPassword;
		IUserDao userDao = new UserDao();
		
		isEqualPwd=StringUtil.checkEquality(userBean.getNewPassword(), userBean.getRepeatedPassword());
		if(isEqualPwd){
			try {
				encodedPassword = EncryptionDecryption.encrypt(userBean
						.getPassword());
				
				encodedNewPassword = EncryptionDecryption.encrypt(userBean
						.getNewPassword());
				
				updatePassword=userDao.updatePassword(userBean.getUserId(),encodedPassword,encodedNewPassword);
			
			} catch (ApplicationException ae) {
				throw ae;
			} catch (Exception e) {
				logger.error("UserService : updatePassword() ----> "
						+ e.getMessage());
				throw new ApplicationException(
						ExceptionMessage.UPDATE_PASSWORD_UNABLE);
			}
		}				
		return updatePassword;
	}

	public boolean deleteUser(String userId) throws ApplicationException {
		boolean isDeleteUser=true;
		IUserDao userDao = new UserDao();
		try{
			isDeleteUser=userDao.deleteUserOnId(userId);
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("UserService : deleteUser() ----> "
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.DELETE_USER_UNABLE);
		}
		return isDeleteUser;
	}

	public boolean addUser(UserBean userBean) throws ApplicationException {
		IUserDao userDao = new UserDao();
		boolean isAddedUser=false;
		try{
			isAddedUser=userDao.saveUser(userBean);
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("UserService : saveUser() ----> "
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.USER_SAVE_UNABLE);
		}
		return isAddedUser;
		
	}

	public boolean changeUserRole(String userId, String userRole)
			throws ApplicationException {
		IUserDao userDao = new UserDao();
		boolean isUpdateUser=false;
		try{
			isUpdateUser=userDao.updateUser(userId,userRole);
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("UserService : changeUserRole() ----> "
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.USER_UPDATE_UNABLE);
		}
		return isUpdateUser;
		
	}

	public List<Jbsult> getUsers() throws ApplicationException {
		IUserDao userDao = new UserDao();
		List<Jbsult> usersList=new ArrayList<Jbsult>();
		try{
			usersList=userDao.getAllUsers();
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("UserService : getUsers() ----> "
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.USERS_UNABLE);
		}
		return usersList;
	}

}
